﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MLY.Bll
{
    public class ClsMailTemplate
    {
          #region Constructors
        // ==================================
        public ClsMailTemplate()
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableMailTemplate();
            IsExist = false;
        }

        public ClsMailTemplate(int MsgId)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableMailTemplates
                        where (o.ID == MsgId)
                        select o).FirstOrDefault();

            IsExist = true;
        }

        public ClsMailTemplate(Utils.MailTemplate templateEnum)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableMailTemplates
                        where (o.ID == (int)templateEnum)
                        select o).FirstOrDefault();

            IsExist = true;
        }

        // ==================================
        #endregion

        #region Static Members
        // ==================================

    

       

        // ==================================
        #endregion

        #region Public Methods
        // ==================================

        public string getHtmlWithContent(string content)
        {
            return Data.TemplateHTML.Replace("$content", content);
        }


        public void Save()
        {
            if (!IsExist)
            {
                _db.TableMailTemplates.InsertOnSubmit(_DataRow);
            }
            _db.SubmitChanges();
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableMailTemplate Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableMailTemplate _DataRow = null;
        // ==================================
        #endregion
    }
}
